package com.ydd.controller.user;

import com.ydd.context.BaseContext;
import com.ydd.dto.ArticleAllPageQueryDTO;
import com.ydd.dto.ArticleCollectOperateDTO;
import com.ydd.dto.ArticleCollectPageQueryDTO;
import com.ydd.dto.ArticleDTO;
import com.ydd.result.PageResult;
import com.ydd.result.Result;
import com.ydd.service.UserService;
import com.ydd.service.YddArticleService;
import com.ydd.vo.PageResultListVO;
import com.ydd.vo.UserVO;
import com.ydd.vo.YddUserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description TODO
 * @Author fang
 * @Date 2023-05-29 16:44
 */
@Api(tags = "用户端-用户相关接口")
@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private YddArticleService articleService;

    @ApiOperation("浏览我的个人文章接口")
    @GetMapping("/myArticle")
    public Result<PageResult> myArticle(ArticleAllPageQueryDTO articleAllPageQueryDTO){
        log.info("浏览我的个人文章接口：{}",articleAllPageQueryDTO);
        PageResult pageResult = articleService.myArticle(articleAllPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("浏览我的收藏/点赞文章接口")
    @GetMapping("/userDataList")
    public Result<PageResultListVO> userDataList(ArticleCollectPageQueryDTO articleCollectPageQueryDTO){
        log.info("浏览我的收藏/点赞文章接口：{}",articleCollectPageQueryDTO);
        PageResult pageResult = articleService.userDataList(articleCollectPageQueryDTO);
        PageResultListVO pageResultListVO = new PageResultListVO(pageResult);
        return Result.success(pageResultListVO);
    }

    @ApiOperation("修改文章接口")
    @PutMapping("/editArticle")
    public Result editArticle(@RequestBody ArticleDTO articleDTO){
        log.info("修改文章接口：{}",articleDTO);
        articleService.update(articleDTO);
        return Result.success();
    }

    @ApiOperation("修改用户资料接口")
    @PostMapping("/update")
    public Result update(@RequestBody UserVO userVO){
        log.info("修改用户资料接口：{}",userVO);
        userService.update(userVO);
        return Result.success();
    }

    @ApiOperation("用户点赞/取消点赞和收藏/取消收藏接口")
    @GetMapping("/userDataHandle")
    public Result userDataHandle(ArticleCollectOperateDTO articleCollectOperateDTO){
        log.info("用户点赞/取消点赞和收藏/取消收藏接口：{}",articleCollectOperateDTO);
        articleService.userDataHandle(articleCollectOperateDTO);
        return Result.success();
    }

    @ApiOperation("发布文章接口")
    @PostMapping("/addArticle")
    public Result addArticle(@RequestBody ArticleDTO articleDTO){
        log.info("发布文章接口：{}",articleDTO);
        articleService.addArticle(articleDTO);
        return Result.success();
    }

    @ApiOperation("删除文章")
    @GetMapping("/delArticle")
    public Result delArticle(Long id){
        log.info("删除文章id：{}",id);
        articleService.del(id.toString());
        return Result.success();
    }

    @ApiOperation("获取用户信息接口")
    @GetMapping("/getUserInfo")
    public Result<YddUserInfoVO> getUserInfo(){
        log.info("获取用户信息接口");
        Long yddUserId = BaseContext.getCurrentId();
        YddUserInfoVO yddUserInfoVO = userService.getUserInfo(yddUserId);
        return Result.success(yddUserInfoVO);
    }
}
